package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component

public class UserController {
    @Autowired
    private IUserMapper userMapper;
    public void start() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入序号：");
        System.out.println("1：用户注册");
        System.out.println("2：用户登录");
        System.out.println("3：修改密码");
        System.out.println("4：用户列表");
        //System.out.println("5.....");
        int num = scanner.nextInt();
        switch (num) {
            case 1:
                regUser();
                break;
            case 2:
                login();
                break;
            case 3:
                System.out.println("修改密码");
            case 4:
                check();
                break;
        }
    }

    private void regUser() {
        //System.out.println("用户注册");
        UserInfo userInfo = new UserInfo();
        System.out.println("欢迎注册");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        userInfo.setUsername(scanner.nextLine());
        //String username = scanner.nextLine();
        System.out.println("请输入密码:");
        userInfo.setPassword(scanner.nextLine());
        System.out.println("请输入昵称:");
        userInfo.setNickname(scanner.nextLine());
        System.out.println("请输入年龄:");
        userInfo.setAge(scanner.nextInt());
        System.out.println(userInfo);
        //System.out.println("用户名：" + username + "，密码：" + password + "，昵称：" + nickname + "，年龄：" + age);


        int num = userMapper.insert(userInfo);
        if (num>0){
            System.out.println("注册成功");
        }else{
            System.out.println("注册失败");
        }
    }
    public void login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输你的信息");
        UserInfo userInfo = new UserInfo();
        System.out.println("输入用户名：");
        userInfo.setUsername(scanner.nextLine());
        System.out.println("输入密码：");
        userInfo.setPassword(scanner.nextLine());

        boolean result = userMapper.query(userInfo);
        if (result) {
            System.out.println("登陆成功，欢迎回来："+userInfo.getNickname());
            System.out.println(userInfo);
        } else {
            System.out.println("登陆失败");
        }
    }
    public void check() {
        System.out.println("用户列表");
        List<UserInfo> list = userMapper.selectAll();
        for (UserInfo userInfo : list) {
            System.out.println(userInfo);
        }
    }
}
